//全部商机

const router = require('koa-router')()
const { yeji } = require('../model')

// 添加数据
router.post('/yeji/add', async (ctx, next) => {
  const {name,contact,tel,ascription,type,go,day1,day2,day3,day4,yue1,yue2,yue3,yue4,yue5,yue6,yue7,yue8,yue9,yue10,yue11,yue12} = ctx.request.body
  const res = await yeji.create({name,contact,tel,ascription,type,go,day1,day2,day3,day4,yue1,yue2,yue3,yue4,yue5,yue6,yue7,yue8,yue9,yue10,yue11,yue12})
  if (res) {
    ctx.body = {
      code: 200,
      msg: '添加成功',
      name,contact,tel,ascription,type,go,day1,day2,day3,day4,yue1,yue2,yue3,yue4,yue5,yue6,yue7,yue8,yue9,yue10,yue11,yue12
    }
  }
})

// 获取全部数据
router.post('/yeji/getAll', async (ctx, next) => {
  const res = await yeji.find();
  console.log(res);
  if (res) {
    ctx.body = {
      code: 200,
      msg: '获取全部数据成功',
      data: res
    }
  }
})

// 修改数据
router.post('/yeji/update', async (ctx, next) => {
  const {ids,name,contact,tel,ascription,type,go,day1,day2,day3,day4,yue1,yue2,yue3,yue4,yue5,yue6,yue7,yue8,yue9,yue10,yue11,yue12  } = ctx.request.body
  let res = await yeji.updateOne(
    { _id: ids },
    {name,contact,tel,ascription,type,go,day1,day2,day3,day4,yue1,yue2,yue3,yue4,yue5,yue6,yue7,yue8,yue9,yue10,yue11,yue12} 
  )
  if (res) {
    ctx.body = {
      code: 200,
      msg: '修改成功',
      data: res,
    }
  }
})

//删除
router.post('/yeji/del', async (ctx, next) =>{
  const { id } = ctx.request.body
  const res = await yeji.deleteOne({ _id: id })

  if (res) {
      ctx.body = {
          code: 200,
          msg: '删除成功',
          data: res
      }
  }
})

module.exports = router